package com.cx.bank.servlet.admin;

import com.cx.bank.handler.MyInVocationHandler;
import com.cx.bank.manager.IManager;
import com.cx.bank.manager.ManagerImpl;
import com.cx.bank.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * 根据userId删除user
 * @author jxau_scf
 * @date 2021/8/5 - 15:23
 */
@WebServlet(urlPatterns = "/user/doDeleteUser")
public class DoDeleteUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userId = req.getParameter("id");
        //获取代理对象，方便使用日志
        IManager manager = new ManagerImpl();
        User user = null;
        try {
            user = manager.findUserByUserId(Integer.valueOf(userId));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        MyInVocationHandler handler = new MyInVocationHandler();
        handler.setUserName(user.getUserName());
        handler.setTarget(manager);
        IManager proxy = (IManager)handler.getProxy();

        boolean deleteUserStatus = proxy.deleteUserByUserId(Integer.valueOf(userId));
        if (deleteUserStatus){
            //删除用户信息成功后
            List<User> userList = null;
            try {
                userList = proxy.findAllUser();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            System.out.println(userList.toString());
            req.setAttribute("userlist", userList);
            //服务端跳转（转发）
            req.getRequestDispatcher("/WEB-INF/jsp/user/admin/allUser.jsp").forward(req, resp);
        }else {
            //删除失败
            req.getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
